//
//  WLHelp.m
//  WLCommon_Example
//
//  Created by meixf on 2017/9/25.
//  Copyright © 2017年 linyinggaoseng. All rights reserved.
//

#import "WLHelp.h"
#import "Jastor.h"
#import "WLConst.h"
@implementation WLHelp
+(id)jsonConvertObject:(NSString *)jsonString WithClassName:(Class)className{
    if (nil == jsonString) {
        return nil;
    }
    NSData *respData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:respData options:NSJSONReadingMutableLeaves error:&error];
    id entity = nil;
    @try {
        entity = [[className alloc] initWithDictionary:rootDic];
    }
    @catch (NSException *exception) {
        WLLOG(@"%@", exception);
    }
    @finally {
        
    }
    return entity;
}

+(id)jsonConvertDictionary:(NSString *)jsonString
{
    if (nil == jsonString) {
        return nil;
    }
    NSData *respData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:respData options:NSJSONReadingMutableLeaves error:&error];
    return rootDic;
}

+(id)objectConvertJson:(id)obj{
    NSString *ret = nil;
    if (nil == obj) {
        return ret;
    }
    if (![obj isKindOfClass:[NSString class]] && ![obj isKindOfClass:[NSNumber class]]) {
        NSError *error = nil;
        if ([obj isKindOfClass:[Jastor class]]) {
            NSDictionary *dic = [obj toDictionary];
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&error];
            obj = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        } else {
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:0 error:&error];
            obj = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
    }
    ret = [NSString stringWithFormat:@"%@", obj];
    return ret;
}
@end
